home *** CD-ROM | disk | FTP | other *** search
/ CD Ware Multimedia 1994 November / Cd Ware (Nro. 2) - Epimundo.iso / DOS / PG / DBPP20.ZIP / EXAMPLE3.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-09  |  2.6 KB  |  97 lines

  1. /*
  2.    DataBase ++ 2.00.
  3.  
  4.    The following example creates a database and fills it with 10 records.
  5.    It creates two index tag and then seeks a name in the file. It reports
  6.    what record it was found at along with some file inormation.
  7.    
  8.    Borland compile ( c4fox.lib == your codebase lib for FoxPro files ):
  9.  
  10.       bcc -ml -DS4FOX example3 dbpp200.lib c4fox.lib emu.lib mathl.lib cl.lib
  11. */
  12.  
  13. #include <dbppdata.hpp>             // DataBase++
  14. #include <iostream.h>
  15.  
  16. #define MAX_NAMES    30
  17. #define FNAME        "people.dbf"
  18.  
  19. void main()
  20. {
  21.    int i;
  22.    
  23.    // Create the DBDatabase object with the file name.
  24.    DBDatabase db( FNAME );
  25.  
  26.    // Create a DBStructure object and add fields to it.
  27.    DBStructure dbs;
  28.    dbs.addField( "name", 'C', 20 );
  29.    dbs.addField( "age", 'N', 3 );
  30.  
  31.    // Create a DBIndexTag object and fill it with tag info.
  32.    DBIndexTag idx;
  33.    idx.addTag( "people", "name" );
  34.    idx.addTag( "age", "age" );
  35.  
  36.    // Create the database and open it.
  37.    if ( ! db.create( dbs, idx ) )
  38.    {
  39.       cout << "Error creating file\n";
  40.       exit( 1 );
  41.    }
  42.    
  43.    // Create some names.
  44.    char *names[ MAX_NAMES ] = {
  45.       "Jeff", "Wendy", "Kyle", "Nicole", "Scott",
  46.       "Andy", "Dave", "Bruce", "Done", "Henry"
  47.    };
  48.  
  49.    // Open index files.  Only needed if NOT using FoxPro .CDX files.
  50. //   db.openIndex( "people.ndx" );
  51. //   db.openIndex( "age.ndx" );
  52.    
  53.    // Add some records.
  54.    for ( i = 0; i < MAX_NAMES; i++ )
  55.    {
  56.       // Append a blank record.
  57.       db.append();
  58.  
  59.       // Replace a field with a string.
  60.       db.replace( "name", names[ i ] );
  61.  
  62.       // Replace a field with an int.
  63.       db.replace( "age", i * 10 );
  64.    }
  65.  
  66.    // Display some info.
  67.    cout << "Data File Name:  " << db.fileName() << endl;
  68.    cout << "Records in File: " << db.reccount() << endl;
  69.    cout << "Current Record:  " << db.recno() << endl;
  70.    cout << "Current Index:   " << db.indexTag() << endl;
  71.  
  72.    cout << endl;
  73.  
  74.    // Set the current index.
  75.    db.setIndexTag( "people" );
  76.    
  77.    // Seek a name.
  78.    cout << "Performing seek()\n";
  79.    if ( db.seek( "Nicole" ) )
  80.       cout << "Nicole found at record " << db.recno() << endl;
  81.    else
  82.       cout << "Nicole not found.\n";
  83.  
  84.    // Seek on an age. Even though the age field is numeric, we can seek
  85.    // by using a string.
  86.    db.setIndexTag( "age" );
  87.    if ( db.seek( "70" ) )
  88.       cout << "Age 70 found at record " << db.recno() << " and belongs to "
  89.            << db.getString( "name" ) << endl;
  90.    else
  91.       cout << "Age 70 not found." << endl;
  92.    
  93.    // Close file.
  94.    db.close();
  95. }
  96.  
  97.